package org.fh.controller.fhoa;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.fh.controller.act.AcStartController;
import org.fh.entity.Page;
import org.fh.entity.PageData;
import org.fh.service.fhoa.SvcGeroupService;
import org.fh.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Controller
@RequestMapping("/svcgeroup")
public class SvcGeroupController extends AcStartController {

    @Autowired
    private SvcGeroupService svcGeroupService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 保存
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/add")
    @RequiresPermissions("svcgeroup:add")
    @ResponseBody
    public Object add(
            @RequestParam(value = "FIMGA", required = false) MultipartFile filea,
            @RequestParam(value = "FIMGB", required = false) MultipartFile fileb,
            @RequestParam(value = "FIMGC", required = false) MultipartFile filec,
            @RequestParam(value = "FIMGD", required = false) MultipartFile filed,
            @RequestParam(value = "FIMGE", required = false) MultipartFile filee,
            @RequestParam(value = "FIMGF", required = false) MultipartFile filef,
            @RequestParam(value = "FIMGG", required = false) MultipartFile fileg,
            @RequestParam(value = "FIMGH", required = false) MultipartFile fileh,
            @RequestParam(value = "FIMGI", required = false) MultipartFile filei,
            @RequestParam(value = "FIMGJ", required = false) MultipartFile filej,
            @RequestParam(value = "FIMGK", required = false) MultipartFile filek,
            @RequestParam(value = "FIMGL", required = false) MultipartFile filel,
            @RequestParam(value = "FIMGM", required = false) MultipartFile filem,
            @RequestParam(value = "FIMGN", required = false) MultipartFile filen,
            @RequestParam(value = "FIMGO", required = false) MultipartFile fileo,
            @RequestParam(value = "FIMGP", required = false) MultipartFile filep,
            @RequestParam(value = "BEIGINTIME", required = false) String BEIGINTIME,
            @RequestParam(value = "ENDTIME", required = false) String ENDTIME,
            @RequestParam(value = "SVCTITLE", required = false) String SVCTITLE,
            @RequestParam(value = "SVCTYPE", required = false) String SVCTYPE,
            @RequestParam(value = "SVCDESC", required = false) String SVCDESC,
            @RequestParam(value = "AMOUNT", required = false) String AMOUNT
    ) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        String ffile = DateUtil.getDays(), fileNamea = "", fileNameb = "", fileNamec = "", fileNamed = "", fileNamee = "", fileNamef = "", fileNameg = "", fileNameh = "", fileNamei = "", fileNamej = "", fileNamek = "", fileNamel = "", fileNamem = "", fileNamen = "", fileNameo = "", fileNamep = "";
        String filePath = PathUtil.getHttpUrlpathFile() + Const.FILEPATHIMG + ffile;     //文件上传路径
        String readFilePath = PathUtil.getHttpUrlpath(request) + Const.FILEPATHIMG + ffile;


        if (null != filea && !filea.isEmpty()) {
            fileNamea = FileUpload.fileUp(filea, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDA", readFilePath + "/" + fileNamea);
        }
        if (null != fileb && !fileb.isEmpty()) {
            fileNameb = FileUpload.fileUp(fileb, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDB", readFilePath + "/" + fileNameb);
        }
        if (null != filec && !filec.isEmpty()) {
            fileNamec = FileUpload.fileUp(filec, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDC", readFilePath + "/" + fileNamec);
        }
        if (null != filed && !filed.isEmpty()) {
            fileNamed = FileUpload.fileUp(filed, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDD", readFilePath + "/" + fileNamed);
        }
        if (null != filee && !filee.isEmpty()) {
            fileNamee = FileUpload.fileUp(filee, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDE", readFilePath + "/" + fileNamee);
        }
        if (null != filef && !filef.isEmpty()) {
            fileNamef = FileUpload.fileUp(filef, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDF", readFilePath + "/" + fileNamef);
        }
        if (null != fileg && !fileg.isEmpty()) {
            fileNameg = FileUpload.fileUp(fileg, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDG", readFilePath + "/" + fileNameg);
        }
        if (null != fileh && !fileh.isEmpty()) {
            fileNameh = FileUpload.fileUp(fileh, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDH", readFilePath + "/" + fileNameh);
        }
        if (null != filei && !filei.isEmpty()) {
            fileNamei = FileUpload.fileUp(filei, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDI", readFilePath + "/" + fileNamei);
        }
        if (null != filej && !filej.isEmpty()) {
            fileNamej = FileUpload.fileUp(filej, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDJ", readFilePath + "/" + fileNamej);
        }
        if (null != filek && !filek.isEmpty()) {
            fileNamek = FileUpload.fileUp(filek, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDK", readFilePath + "/" + fileNamek);
        }
        if (null != filel && !filel.isEmpty()) {
            fileNamel = FileUpload.fileUp(filel, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDL", readFilePath + "/" + fileNamel);
        }
        if (null != filem && !filem.isEmpty()) {
            fileNamem = FileUpload.fileUp(filem, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDM", readFilePath + "/" + fileNamem);
        }
        if (null != filen && !filen.isEmpty()) {
            fileNamen = FileUpload.fileUp(filen, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDN", readFilePath + "/" + fileNamen);
        }
        if (null != fileo && !fileo.isEmpty()) {
            fileNameo = FileUpload.fileUp(fileo, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDO", readFilePath + "/" + fileNameo);
        }
        if (null != filep && !filep.isEmpty()) {
            fileNamep = FileUpload.fileUp(filep, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDP", readFilePath + "/" + fileNamep);
        }

        //写入表
        pd.put("BEIGINTIME", BEIGINTIME);
        pd.put("ENDTIME", ENDTIME);
        pd.put("SVCTITLE", SVCTITLE);
        pd.put("SVCTYPE", SVCTYPE);
        pd.put("SVCDESC", SVCDESC);
        pd.put("AMOUNT", AMOUNT);
        pd.put("CTIME", DateUtil.date2Str(new Date()));    //创建时间
        pd.put("USERNAME", Jurisdiction.getUsername());    //
        pd.put("SVCGEROUP_ID", this.get32UUID());                    //主键
        pd.put("UNAME", Jurisdiction.getName());
        pd.put("USERNAME", Jurisdiction.getUsername());

        pd.put("DEPARTMENT_ID", "");
//			pd.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());


        svcGeroupService.save(pd);

        try {
            /** 工作流的操作 **/
            Map<String, Object> mapa = new LinkedHashMap<String, Object>();
            mapa.put("申请人员", Jurisdiction.getName());            //当前用户的姓名
            mapa.put("开始时间", BEIGINTIME);
            mapa.put("结束时间", ENDTIME);
            mapa.put("申请标题", SVCTITLE);
            mapa.put("申请类型", SVCTYPE);
            mapa.put("申请内容", SVCDESC);
            mapa.put("申请金额", AMOUNT);
            String USERNAME = Jurisdiction.getUsername();
            mapa.put("USERNAME", USERNAME);                        //指派代理人为当前用户
            startProcessInstanceByKeyHasVariables("KEY_leavesbx", mapa, USERNAME);//启动流程实例(请假单流程)通过KEY
            //记录存入数据库
            map.put("ASSIGNEE_", Jurisdiction.getUsername());                //用于给待办人发送新任务消息
        } catch (Exception e) {
            errInfo = "errer";
        }

        map.put("result", errInfo);                //返回结果
        return map;
    }


    /**
     * 删除
     *
     * @param out
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @RequiresPermissions("svcgeroup:del")
    @ResponseBody
    public Object delete() throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        svcGeroupService.delete(pd);
        map.put("result", errInfo);                //返回结果
        return map;
    }


    /**
     * 修改
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/edit")
    @RequiresPermissions("svcgeroup:edit")
    @ResponseBody
    public Object edit(
            @RequestParam(value = "PHOTOADDA", required = false) MultipartFile filea,
            @RequestParam(value = "PHOTOADDB", required = false) MultipartFile fileb,
            @RequestParam(value = "PHOTOADDC", required = false) MultipartFile filec,
            @RequestParam(value = "PHOTOADDD", required = false) MultipartFile filed,
            @RequestParam(value = "PHOTOADDE", required = false) MultipartFile filee,
            @RequestParam(value = "PHOTOADDF", required = false) MultipartFile filef,
            @RequestParam(value = "PHOTOADDG", required = false) MultipartFile fileg,
            @RequestParam(value = "PHOTOADDH", required = false) MultipartFile fileh,
            @RequestParam(value = "PHOTOADDI", required = false) MultipartFile filei,
            @RequestParam(value = "PHOTOADDJ", required = false) MultipartFile filej,
            @RequestParam(value = "PHOTOADDK", required = false) MultipartFile filek,
            @RequestParam(value = "PHOTOADDL", required = false) MultipartFile filel,
            @RequestParam(value = "PHOTOADDM", required = false) MultipartFile filem,
            @RequestParam(value = "PHOTOADDN", required = false) MultipartFile filen,
            @RequestParam(value = "PHOTOADDO", required = false) MultipartFile fileo,
            @RequestParam(value = "PHOTOADDP", required = false) MultipartFile filep,
            @RequestParam(value = "BEIGINTIME", required = false) String BEIGINTIME,
            @RequestParam(value = "ENDTIME", required = false) String ENDTIME,
            @RequestParam(value = "SVCTITLE", required = false) String SVCTITLE,
            @RequestParam(value = "SVCTYPE", required = false) String SVCTYPE,
            @RequestParam(value = "SVCDESC", required = false) String SVCDESC,
            @RequestParam(value = "AMOUNT", required = false) String AMOUNT,
            @RequestParam(value = "SVCGEROUP_ID", required = false) String SVCGEROUP_ID
    ) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        String ffile = DateUtil.getDays(), fileNamea = "", fileNameb = "", fileNamec = "", fileNamed = "", fileNamee = "", fileNamef = "", fileNameg = "", fileNameh = "", fileNamei = "", fileNamej = "", fileNamek = "", fileNamel = "", fileNamem = "", fileNamen = "", fileNameo = "", fileNamep = "";
        String filePath = PathUtil.getHttpUrlpathFile() + Const.FILEPATHIMG + ffile;     //文件上传路径
        String readFilePath = PathUtil.getHttpUrlpath(request) + Const.FILEPATHIMG + ffile;


        if (null != filea && !filea.isEmpty()) {
            fileNamea = FileUpload.fileUp(filea, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDA", readFilePath + "/" + fileNamea);
        }
        if (null != fileb && !fileb.isEmpty()) {
            fileNameb = FileUpload.fileUp(fileb, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDB", readFilePath + "/" + fileNameb);
        }
        if (null != filec && !filec.isEmpty()) {
            fileNamec = FileUpload.fileUp(filec, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDC", readFilePath + "/" + fileNamec);
        }
        if (null != filed && !filed.isEmpty()) {
            fileNamed = FileUpload.fileUp(filed, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDD", readFilePath + "/" + fileNamed);
        }
        if (null != filee && !filee.isEmpty()) {
            fileNamee = FileUpload.fileUp(filee, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDE", readFilePath + "/" + fileNamee);
        }
        if (null != filef && !filef.isEmpty()) {
            fileNamef = FileUpload.fileUp(filef, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDF", readFilePath + "/" + fileNamef);
        }
        if (null != fileg && !fileg.isEmpty()) {
            fileNameg = FileUpload.fileUp(fileg, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDG", readFilePath + "/" + fileNameg);
        }
        if (null != fileh && !fileh.isEmpty()) {
            fileNameh = FileUpload.fileUp(fileh, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDH", readFilePath + "/" + fileNameh);
        }
        if (null != filei && !filei.isEmpty()) {
            fileNamei = FileUpload.fileUp(filei, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDI", readFilePath + "/" + fileNamei);
        }
        if (null != filej && !filej.isEmpty()) {
            fileNamej = FileUpload.fileUp(filej, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDJ", readFilePath + "/" + fileNamej);
        }
        if (null != filek && !filek.isEmpty()) {
            fileNamek = FileUpload.fileUp(filek, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDK", readFilePath + "/" + fileNamek);
        }
        if (null != filel && !filel.isEmpty()) {
            fileNamel = FileUpload.fileUp(filel, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDL", readFilePath + "/" + fileNamel);
        }
        if (null != filem && !filem.isEmpty()) {
            fileNamem = FileUpload.fileUp(filem, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDM", readFilePath + "/" + fileNamem);
        }
        if (null != filen && !filen.isEmpty()) {
            fileNamen = FileUpload.fileUp(filen, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDN", readFilePath + "/" + fileNamen);
        }
        if (null != fileo && !fileo.isEmpty()) {
            fileNameo = FileUpload.fileUp(fileo, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDO", readFilePath + "/" + fileNameo);
        }
        if (null != filep && !filep.isEmpty()) {
            fileNamep = FileUpload.fileUp(filep, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDP", readFilePath + "/" + fileNamep);
        }


        pd.put("BEIGINTIME", BEIGINTIME);
        pd.put("ENDTIME", ENDTIME);
        pd.put("SVCTITLE", SVCTITLE);
        pd.put("SVCTYPE", SVCTYPE);
        pd.put("SVCDESC", SVCDESC);
        pd.put("AMOUNT", AMOUNT);
        pd.put("CTIME", DateUtil.date2Str(new Date()));    //创建时间
        pd.put("USERNAME", Jurisdiction.getUsername());    //
        pd.put("SVCGEROUP_ID", SVCGEROUP_ID);                    //主键
//		pd.put("UNAME", Jurisdiction.getName());
//		pd.put("USERNAME", Jurisdiction.getUsername());
//		pd.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
        svcGeroupService.edit(pd);

        map.put("result", errInfo);                //返回结果
        return map;
    }

    /**
     * 去修改页面
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/goEdit")
    @ResponseBody
    public Object goEdit() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd = svcGeroupService.findById(pd);    //根据ID读取
        map.put("pd", pd);
        map.put("result", errInfo);                //返回结果
        return map;
    }

    /**
     * 列表
     *
     * @param page
     * @throws Exception
     */
    @RequestMapping(value = "/list")
    @RequiresPermissions("svcgeroup:list")
    @ResponseBody
    public Object list(Page page) throws Exception {
//		Map<String,Object> map = new HashMap<String,Object>();
//		String errInfo = "success";
//		PageData pd = new PageData();
//		pd = this.getPageData();
//
//		String USERNAME = Jurisdiction.getUsername();					//用户名
//		page.setPd(pd);
//		List<PageData>	varList = svcGeroupService.list(page);
//		map.put("varList", varList);
//		map.put("page", page);
//		map.put("USERNAME", USERNAME);
//		map.put("result", errInfo);
//		return map;

        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String isMy = pd.getString("isMy");
        String KEYWORDS = pd.getString("KEYWORDS");                        //关键词检索条件
        if (Tools.notEmpty(KEYWORDS)) pd.put("KEYWORDS", KEYWORDS.trim());
        String lastStart = pd.getString("lastStart");                    //开始时间
        String lastEnd = pd.getString("lastEnd");                        //结束时间
        if (Tools.notEmpty(lastStart)) pd.put("lastStart", lastStart + " 00:00:00");
        if (Tools.notEmpty(lastEnd)) pd.put("lastEnd", lastEnd + " 00:00:00");
        String USERNAME = Jurisdiction.getUsername();                    //用户名
        String item = Jurisdiction.getDEPARTMENT_IDS();
        if ("false".equals(isMy)) {
            if ("".equals(item) || "无权".equals(item)) {
                pd.put("item", "");                                            //根据部门ID过滤
                if (!"admin".equals(USERNAME)) pd.put("USERNAME", USERNAME);    //非admin用户时
            } else {
                pd.put("ORUSERNAME", USERNAME);
                pd.put("item", item.replaceFirst("\\(", "\\('" + Jurisdiction.getDEPARTMENT_ID() + "',"));
            }
        } else {
            pd.put("USERNAME", USERNAME);
        }
        page.setPd(pd);

        List<PageData> varList = svcGeroupService.list(page);            //列出Worklog列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("USERNAME", USERNAME);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 导出到excel
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/excel")
    @RequiresPermissions("toExcel")
    public ModelAndView exportExcel() throws Exception {
        ModelAndView mv = new ModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        List<String> titles = new ArrayList<String>();
        titles.add("开始时间");    //1
        titles.add("结束时间");    //2
        titles.add("标题");    //3
        titles.add("类型");    //4
        titles.add("填写说明");    //5
        titles.add("总金额");    //6
        titles.add("上传照片地址一");    //7
        titles.add("上传照片地址二");    //8
        titles.add("上传照片地址三");    //9
        titles.add("上传照片地址四");    //10
        titles.add("上传照片地址五");    //11
        titles.add("上传照片地址六");    //11
        titles.add("上传照片地址七");    //11
        titles.add("上传照片地址八");    //11
        titles.add("上传照片地址九");    //11
        titles.add("上传照片地址十");    //11
        titles.add("上传照片地址十一");    //11
        titles.add("上传照片地址十二");    //11
        titles.add("上传照片地址十三");    //11
        titles.add("上传照片地址十四");    //11
        titles.add("上传照片地址十五");    //11
        titles.add("上传照片地址十六");    //11
        titles.add("姓名");    //12
        titles.add("创建时间");    //13
        dataMap.put("titles", titles);
        List<PageData> varOList = svcGeroupService.listAll(pd);
        List<PageData> varList = new ArrayList<PageData>();
        for (int i = 0; i < varOList.size(); i++) {
            PageData vpd = new PageData();
            vpd.put("var1", varOList.get(i).getString("BEIGINTIME"));        //1
            vpd.put("var2", varOList.get(i).getString("ENDTIME"));        //2
            vpd.put("var3", varOList.get(i).getString("SVCTITLE"));        //2
            vpd.put("var4", varOList.get(i).getString("SVCTYPE"));        //2
            vpd.put("var5", varOList.get(i).getString("SVCDESC"));        //2
            vpd.put("var6", varOList.get(i).getString("AMOUNT"));        //2
            vpd.put("var7", varOList.get(i).getString("PHOTOADDA"));        //2
            vpd.put("var8", varOList.get(i).getString("PHOTOADDB"));        //2
            vpd.put("var9", varOList.get(i).getString("PHOTOADDC"));        //2
            vpd.put("var10", varOList.get(i).getString("PHOTOADDD"));        //2
            vpd.put("var11", varOList.get(i).getString("PHOTOADDE"));        //2
            vpd.put("var11", varOList.get(i).getString("PHOTOADDF"));        //2
            vpd.put("var11", varOList.get(i).getString("PHOTOADDG"));        //2
            vpd.put("var11", varOList.get(i).getString("PHOTOADDH"));        //2
            vpd.put("var11", varOList.get(i).getString("PHOTOADDI"));        //2
            vpd.put("var11", varOList.get(i).getString("PHOTOADDJ"));        //2
            vpd.put("var11", varOList.get(i).getString("PHOTOADDK"));        //2
            vpd.put("var11", varOList.get(i).getString("PHOTOADDL"));        //2
            vpd.put("var11", varOList.get(i).getString("PHOTOADDM"));        //2
            vpd.put("var11", varOList.get(i).getString("PHOTOADDN"));        //2
            vpd.put("var11", varOList.get(i).getString("PHOTOADDO"));        //2
            vpd.put("var11", varOList.get(i).getString("PHOTOADDP"));        //2
            vpd.put("var12", varOList.get(i).getString("USERNAME"));        //3
            vpd.put("var13", varOList.get(i).getString("CTIME"));        //4
            varList.add(vpd);
        }
        dataMap.put("varList", varList);
        ObjectExcelView erv = new ObjectExcelView();
        mv = new ModelAndView(erv, dataMap);
        return mv;
    }
}
